import {SimpleGit} from 'simple-git'
import process from 'process'
import * as step from '@flow-step/step-toolkit'
import {masking} from './util'

export async function deleteBranch(git:SimpleGit,branch:string){
    if (branch == 'master') {
        step.info('Current on master branch, skip deleting');
        return;
    }
    if (branch == process.env.TARGET_BRANCH){
        step.info(`Current branch ${branch} is the same as the target branch ${process.env.TARGET_BRANCH}, skip deleting.`);
        return;
    }
    try {
        await git.push('origin', branch, ['--delete']);
        step.info(`branch ${branch} was successfully deleted`);
    }catch (error:any){
        step.error(`failed to delete branch ${branch}`);
        throw new Error(masking(error.message));
    }
}